﻿Imports System.Data.OleDb

Public Class ctrChinhSuaBan

    Private Sub ctrChinhSuaBan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gvBan_Load()
    End Sub

    Private Sub gvBan_Load()
        Dim bus As GeneralBUS = New GeneralBUS()
        gvBan.DataSource = bus.GetTable("select * from BAN where TinhTrang='Trống'")
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim tmp As BanDTO = New BanDTO()
        tmp.TinhTrang = "Trống"

        Dim bus As BanBUS = New BanBUS()
        bus.Insert(tmp)

        gvBan_Load()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim tmp As BanDTO = New BanDTO()
        tmp.MaBan = gvBan.CurrentRow.Cells(0).Value.ToString()

        Dim bus As BanBUS = New BanBUS()
        bus.Delete(tmp)

        gvBan_Load()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        frmMain.TabControl1.TabPages.Remove(frmMain.TabControl1.SelectedTab)
    End Sub

    Private Sub btnNhanBan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhanBan.Click
        Dim tmp As BanDTO = New BanDTO()
        tmp.MaBan = gvBan.CurrentRow.Cells(0).Value.ToString()
        tmp.TinhTrang = "Đầy"

        XoaBanDat(tmp.MaBan.ToString())

        Dim tmp2 As HoaDonDTO = New HoaDonDTO()
        tmp2.MaBan = gvBan.CurrentRow.Cells(0).Value.ToString()
        tmp2.ThoiGian = Date.Now.ToString()
        tmp2.TongTien = 0
        tmp2.TinhTrangTT = False

        Dim bus1 As BanBUS = New BanBUS()
        Dim bus2 As HoaDonBUS = New HoaDonBUS()

        bus2.Insert(tmp2)
        bus1.Update(tmp)
    End Sub

    Private Sub XoaBanDat(ByVal t As String)
        Dim cn As OleDbConnection = DataProvider.ConnectionData()
        Dim str As String = "delete from BANDAT where MaBan=" + t
        Dim cmd As OleDbCommand = New OleDbCommand(str, cn)
        Dim result As Integer = cmd.ExecuteNonQuery()
        cn.Close()
    End Sub
End Class
